package com.example.activitidemo.web;

import com.example.activitidemo.server.ActivitiService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@RestController
@RequestMapping("/activiti")
public class DemoController {
    @Autowired
    private ActivitiService activitiService;


    @GetMapping(value = "/start")
    public String startProcess(String taskType,String businessId, @RequestBody Map<String,Object> params){
        return activitiService.start(taskType,businessId,params);
    }

    @GetMapping(value = "/complete")
    public String complete(String taskId,@RequestBody Map<String,Object> params){
        return activitiService.complete(taskId,params);
    }


    @RequestMapping(value = "list",method = RequestMethod.GET,produces = "application/json")
    public String getList(String userName){
        return activitiService.getTask(userName).toString();
    }

    @PostMapping(value = "/deployBpmn")
    public String deployBpmn(@RequestParam("file") MultipartFile file) {
        try {
            String name = file.getOriginalFilename();
            if (!name.endsWith(".bpmn20.xml") && !name.endsWith(".bpmn")) {
                name = name + ".bpmn";
            }
            return activitiService.deploy(name, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("upload failed");
        }
    }


    @PostMapping(value = "/deploy")
    public String deploy(String name, InputStream inputStream){
        return activitiService.deploy(name, inputStream);
    }


}
